<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html dir="ltr" xml:lang="zh-CN" xmlns="http://www.w3.org/1999/xhtml"><head>

  
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Spring中实现文件上传 - 温柔一刀 - JavaEye技术网站</title>
    <meta name="description" content=" 实现图片上传  　　用户必须能够上传图片，因此需要文件上传的功能。比较常见的文件上传组件有Commons FileUpload（http://jakarta.apache.org/commons/fileupload/a&gt;）和COS FileUpload（http://www.servlets.com/cos），Spring已经完全集成了这两种组件，这里我们选择Commons FileUpl ...">
    <meta name="keywords" content="Spring  Spring中实现文件上传">
    <link rel="shortcut icon" href="http://zhupan.javaeye.com/images/favicon.ico" type="image/x-icon">
    <link href="http://zhupan.javaeye.com/rss" rel="alternate" title="温柔一刀" type="application/rss+xml">
    <link href="26427_files/blog.css" media="screen" rel="stylesheet" type="text/css">
<link href="26427_files/green.css" media="screen" rel="stylesheet" type="text/css">
    <script src="26427_files/application.js" type="text/javascript"></script>
      <link href="26427_files/SyntaxHighlighter.css" media="screen" rel="stylesheet" type="text/css">
  <script src="26427_files/shCoreCommon.js" type="text/javascript"></script>
<script src="26427_files/hotkey.js" type="text/javascript"></script>
  <style>
    div#main {background-color: transparent; border: 0; padding: 0px; width: 740px;}
  </style>
  </head><body>
    <div id="header">
      <div id="blog_site_nav">
  <a href="http://www.javaeye.com/" class="homepage">首页</a>
  <a href="http://www.javaeye.com/news">新闻</a>
  <a href="http://www.javaeye.com/forums">论坛</a>
  <a href="http://www.javaeye.com/blogs">博客</a>
  <a href="http://www.javaeye.com/job">招聘</a>
  <a href="#" onclick="return false;" id="msna"><u>更多</u> <small>▼</small></a>
  <div class="quick_menu" style="display: none;">
    <a href="http://www.javaeye.com/ask">问答</a>
    <a href="http://www.javaeye.com/wiki">知识库</a>
    <a href="http://www.javaeye.com/groups">圈子</a>
    <a href="http://www.javaeye.com/google_search">搜索</a>
  </div>
</div>
      <div id="user_nav">
      <a href="http://zhupan.javaeye.com/login" class="welcome" title="登录">您还未登录 !</a>
    <a href="http://www.javaeye.com/all" style="color: red;">我的应用</a>
    <a href="http://zhupan.javaeye.com/login">登录</a>
    <a href="http://zhupan.javaeye.com/signup" class="nobg">注册</a>
  </div>
    </div>

    <div id="page">
      <div id="branding" class="clearfix">
        <div id="blog_name"><h1><a href="http://zhupan.javaeye.com/">温柔一刀</a></h1></div>
        <div id="blog_preview"></div>
        <div id="blog_domain">永久域名 <a href="http://zhupan.javaeye.com/">http://zhupan.javaeye.com</a></div>
      </div>
      
      
      <div id="content" class="clearfix">
        <div id="main">
          


          


<div class="blog_main"> 
  <div id="blog_nav">
    
    <div id="pre_next">
      <a href="http://zhupan.javaeye.com/blog/22159" class="next" title="spring、ibatis控制oracle分页的问题">spring、ibatis控制oracle分页的问题</a>
      |
      <a href="http://zhupan.javaeye.com/blog/26465" class="pre" title="通过SCJP考试，说点体会和感受">通过SCJP考试，说点体会和感受</a>
    </div>
  </div>
  <div class="blog_title">
    <div class="date"><span class="year">2006</span><span class="sep_year">-</span><span class="month">08</span><span class="sep_month">-</span><span class="day">17</span></div>
    <h3 class="" title=""><a href="http://zhupan.javaeye.com/blog/26427">Spring中实现文件上传</a></h3>
                <strong>关键字: Spring</strong>
  </div>

  <div class="blog_content">
    <div class="postText">
<p>实现图片上传 <br>
　　用户必须能够上传图片，因此需要文件上传的功能。比较常见的文件上传组件有Commons FileUpload（<a href="http://jakarta.apache.org/commons/fileupload/a">http://jakarta.apache.org/commons/fileupload/a</a>&gt;）和COS FileUpload（<a href="http://www.servlets.com/cos">http://www.servlets.com/cos</a>），Spring已经完全集成了这两种组件，这里我们选择Commons FileUpload。 <br>
　　由于Post一个包含文件上传的Form会以multipart/form-data请求发送给服务器，必须明确告诉
DispatcherServlet如何处理MultipartRequest。首先在dispatcher-servlet.xml中声明一个
MultipartResolver：</p>
<div class="code_title">xml 代码</div>
<div class="dp-highlighter">
<div class="bar">
<ol class="dp-xml">
    <li class="alt"><span><span class="tag">&lt;</span><span class="tag-name">bean</span><span>&nbsp;</span><span class="attribute">id</span><span>=</span><span class="attribute-value">"multipartResolver"</span><span>&nbsp;&nbsp;</span></span></li>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="attribute">class</span><span>=</span><span class="attribute-value">"org.springframework.web.multipart.commons.CommonsMultipartResolver"</span><span class="tag">&gt;</span><span>&nbsp;&nbsp;</span></li>
    <li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="comments">&lt;!--&nbsp;设置上传文件的最大尺寸为1MB&nbsp;--&gt;</span><span>&nbsp;&nbsp;</span></li>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="tag">&lt;</span><span class="tag-name">property</span><span>&nbsp;</span><span class="attribute">name</span><span>=</span><span class="attribute-value">"maxUploadSize"</span><span class="tag">&gt;</span><span>&nbsp;&nbsp;</span></li>
    <li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="tag">&lt;</span><span class="tag-name">value</span><span class="tag">&gt;</span><span>1048576</span><span class="tag">&lt;/</span><span class="tag-name">value</span><span class="tag">&gt;</span><span>&nbsp;&nbsp;</span></li>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="tag">&lt;/</span><span class="tag-name">property</span><span class="tag">&gt;</span><span>&nbsp;&nbsp;</span></li>
    <li class="alt"><span><span class="tag">&lt;/</span><span class="tag-name">bean</span><span class="tag">&gt;</span><span>&nbsp;&nbsp;</span></span></li>
</ol>
</div>
<p>&nbsp;这样一旦某个Request是一个MultipartRequest，它就会首先被MultipartResolver处理，然后再转发相应的Controller。 <br>
在UploadImageController中，将HttpServletRequest转型为MultipartHttpServletRequest，就能非常方便地得到文件名和文件内容： </p>
<div class="code_title">java 代码</div>
<div class="dp-highlighter">
<div class="bar">
<ol class="dp-j">
    <li class="alt"><span><span class="keyword">public</span><span>&nbsp;ModelAndView&nbsp;handleRequest(HttpServletRequest&nbsp;request, &nbsp;&nbsp;</span></span></li>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HttpServletResponse&nbsp;response)&nbsp;</span><span class="keyword">throws</span><span>&nbsp;Exception&nbsp;{ &nbsp;&nbsp;</span></li>
    <li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="comment">//&nbsp;转型为MultipartHttpRequest： </span><span>&nbsp;&nbsp;</span></li>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MultipartHttpServletRequest&nbsp;multipartRequest&nbsp;=&nbsp;(MultipartHttpServletRequest)&nbsp;request;
&nbsp;&nbsp;</span></li>
    <li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="comment">//&nbsp;获得文件： </span><span>&nbsp;&nbsp;</span></li>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MultipartFile&nbsp;file&nbsp;=&nbsp;multipartRequest.getFile(</span><span class="string">"&nbsp;file&nbsp;"</span><span>); &nbsp;&nbsp;</span></li>
    <li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="comment">//&nbsp;获得文件名： </span><span>&nbsp;&nbsp;</span></li>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;filename&nbsp;=&nbsp;file.getOriginalFilename(); &nbsp;&nbsp;</span></li>
    <li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="comment">//&nbsp;获得输入流： </span><span>&nbsp;&nbsp;</span></li>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;InputStream&nbsp;input&nbsp;=&nbsp;file.getInputStream(); &nbsp;&nbsp;</span></li>
    <li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="comment">//&nbsp;写入文件 </span><span>&nbsp;&nbsp;</span></li>
    <li class=""><span>&nbsp;&nbsp;</span></li>
    <li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="comment">//&nbsp;或者： </span><span>&nbsp;&nbsp;</span></li>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;File&nbsp;source&nbsp;=&nbsp;</span><span class="keyword">new</span><span>&nbsp;File(localfileName.toString()); &nbsp;&nbsp;</span></li>
    <li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;multipartFile.transferTo(source); &nbsp;&nbsp;</span></li>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li>
</ol>
</div>
<p>生成缩略图 (目录)<br>
　　当用户上传了图片后，必须生成缩略图以便用户能快速浏览。我们不需借助第三方软件，JDK标准库就包含了图像处理的API。我们把一张图片按比例缩放到120X120大小，以下是关键代码：</p>
<div class="code_title">java 代码</div>
<div class="dp-highlighter">
<div class="bar">
<ol class="dp-j">
    <li class="alt"><span><span class="keyword">public</span><span>&nbsp;</span><span class="keyword">static</span><span>&nbsp;</span><span class="keyword">void</span><span>&nbsp;createPreviewImage(String&nbsp;srcFile,&nbsp;String&nbsp;destFile)&nbsp;{ &nbsp;&nbsp;</span></span></li>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="keyword">try</span><span>&nbsp;{ &nbsp;&nbsp;</span></li>
    <li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;File&nbsp;fi&nbsp;=&nbsp;</span><span class="keyword">new</span><span>&nbsp;File(srcFile);&nbsp;</span><span class="comment">//&nbsp;src </span><span>&nbsp;&nbsp;</span></li>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;File&nbsp;fo&nbsp;=&nbsp;</span><span class="keyword">new</span><span>&nbsp;File(destFile);&nbsp;</span><span class="comment">//&nbsp;dest </span><span>&nbsp;&nbsp;</span></li>
    <li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BufferedImage&nbsp;bis&nbsp;=&nbsp;ImageIO.read(fi);
&nbsp;&nbsp;</span></li>
    <li class=""><span>&nbsp;&nbsp;</span></li>
    <li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="keyword">int</span><span>&nbsp;w&nbsp;=&nbsp;bis.getWidth(); &nbsp;&nbsp;</span></li>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="keyword">int</span><span>&nbsp;h&nbsp;=&nbsp;bis.getHeight(); &nbsp;&nbsp;</span></li>
    <li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="keyword">double</span><span>&nbsp;scale&nbsp;=&nbsp;(</span><span class="keyword">double</span><span>)&nbsp;w&nbsp;/&nbsp;h; &nbsp;&nbsp;</span></li>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="keyword">int</span><span>&nbsp;nw&nbsp;=&nbsp;IMAGE_SIZE;&nbsp;</span><span class="comment">//&nbsp;final&nbsp;int&nbsp;IMAGE_SIZE&nbsp;=&nbsp;120; </span><span>&nbsp;&nbsp;</span></li>
    <li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="keyword">int</span><span>&nbsp;nh&nbsp;=&nbsp;(nw&nbsp;*&nbsp;h)&nbsp;/&nbsp;w; &nbsp;&nbsp;</span></li>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="keyword">if</span><span>&nbsp;(nh&nbsp;&gt;&nbsp;IMAGE_SIZE)&nbsp;{ &nbsp;&nbsp;</span></li>
    <li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nh&nbsp;=&nbsp;IMAGE_SIZE;
&nbsp;&nbsp;</span></li>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nw&nbsp;=&nbsp;(nh&nbsp;*&nbsp;w)&nbsp;/&nbsp;h;
&nbsp;&nbsp;</span></li>
    <li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;</span></li>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="keyword">double</span><span>&nbsp;sx&nbsp;=&nbsp;(</span><span class="keyword">double</span><span>)&nbsp;nw&nbsp;/&nbsp;w; &nbsp;&nbsp;</span></li>
    <li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="keyword">double</span><span>&nbsp;sy&nbsp;=&nbsp;(</span><span class="keyword">double</span><span>)&nbsp;nh&nbsp;/&nbsp;h; &nbsp;&nbsp;</span></li>
    <li class=""><span>&nbsp;&nbsp;</span></li>
    <li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;transform.setToScale(sx,&nbsp;sy); &nbsp;&nbsp;</span></li>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AffineTransformOp&nbsp;ato&nbsp;=&nbsp;</span><span class="keyword">new</span><span>&nbsp;AffineTransformOp(transform,&nbsp;</span><span class="keyword">null</span><span>); &nbsp;&nbsp;</span></li>
    <li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BufferedImage&nbsp;bid&nbsp;=&nbsp;</span><span class="keyword">new</span><span>&nbsp;BufferedImage(nw,&nbsp;nh, &nbsp;&nbsp;</span></li>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BufferedImage.TYPE_3BYTE_BGR);
&nbsp;&nbsp;</span></li>
    <li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ato.filter(bis,&nbsp;bid); &nbsp;&nbsp;</span></li>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ImageIO.write(bid,&nbsp;</span><span class="string">"&nbsp;jpeg&nbsp;"</span><span>,&nbsp;fo); &nbsp;&nbsp;</span></li>
    <li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;</span><span class="keyword">catch</span><span>&nbsp;(Exception&nbsp;e)&nbsp;{ &nbsp;&nbsp;</span></li>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.printStackTrace(); &nbsp;&nbsp;</span></li>
    <li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="keyword">throw</span><span>&nbsp;</span><span class="keyword">new</span><span>&nbsp;RuntimeException( &nbsp;&nbsp;</span></li>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="string">"&nbsp;Failed&nbsp;in&nbsp;create&nbsp;preview&nbsp;image.&nbsp;Error:&nbsp;&nbsp;"</span><span>&nbsp;&nbsp;</span></li>
    <li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;+&nbsp;e.getMessage());
&nbsp;&nbsp;</span></li>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;</span></li>
    <li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li>
</ol>
</div>
</div></div></div></div>
  </div>

  

  <div>
      <script type="text/javascript"><!--
      google_ad_client = "pub-8107967023732698";
      google_ad_slot = "3730053242";
      google_ad_width = 728;
      google_ad_height = 15;
      //--></script>
      <script type="text/javascript" src="26427_files/show_ads.js">
      </script><script src="26427_files/expansion_embed.js"></script><script src="26427_files/test_domain.js"></script><script>google_protectAndRun("ads_core.google_render_ad", google_handleError, google_render_ad);</script><ins style="border: medium none ; margin: 0pt; padding: 0pt; display: inline-table; height: 15px; position: relative; visibility: visible; width: 728px;"><ins style="border: medium none ; margin: 0pt; padding: 0pt; display: block; height: 15px; position: relative; visibility: visible; width: 728px;"><iframe allowtransparency="true" hspace="0" id="google_ads_frame1" marginheight="0" marginwidth="0" name="google_ads_frame" src="26427_files/ads.htm" style="left: 0pt; position: absolute; top: 0pt;" vspace="0" width="728" frameborder="0" height="15" scrolling="no"></iframe></ins></ins>  
    </div>

  
  

  <div class="blog_bottom">
    <ul>
      <li>10:28</li>
      <li>浏览 (5529)</li>
      <li><a href="http://www.javaeye.com/topic/26427" title="在论坛被浏览了4793 次">论坛浏览 (4793)</a></li>       <li><a href="#comments">评论</a> (1)</li>
      <li>分类: <a href="http://zhupan.javaeye.com/category/5360">open source  </a></li>
      
      
      
      <li class="last"><a href="http://www.javaeye.com/wiki/topic/26427" target="_blank" class="more">相关推荐</a></li>
    </ul>
  </div>

  <div class="blog_comment">
    <h5>评论</h5>
    <a id="comments" name="comments"></a>
    <div id="bc246356">
  <div class="comment_title">
    1 楼
    <a href="http://benbencin.javaeye.com/" target="_blank">benbencin</a>
    2007-03-22&nbsp;&nbsp;
    <a href="#" onclick="quote_comment(246356);return false;">引用</a>
    
  </div>
  <div class="comment_content">楼主：我现在进行转型时报错了，就是在这一句：
<br>MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; 
<br>跟踪进去之后发现错误描述是这样的：
<br>ClassCastException: Cannot cast
org.apache.coyote.tomcat5.CoyoteRequestFacade (id=90) to
org.springframework.web.multipart.MultipartHttpServletRequest。
<br>我很纳闷，我已经申明了MultipartResolver，也在form设置了enctype="multipart/form-data"，为什么还会发生转型错误呢？
<br></div>
</div>

    
    
  </div>

  <div class="blog_comment">
    <h5>发表评论</h5>
    
      <p style="margin: 5px 0pt; font-weight: bold;">该博客是同时发布到<a href="http://www.javaeye.com/topic/26427">论坛</a>的，无法评论在论坛已被锁定的帖子</p>
      </div>
</div>


<script type="text/javascript">
  dp.SyntaxHighlighter.HighlightAll('code', true, true);

  fix_image_size($$('div.blog_content img'), 700);
  function quote_comment(id) {
    new Ajax.Request('/editor/quote', {
      parameters: {'id':id, 'type':'Post'},
      onSuccess:function(response){editor.bbcode_editor.textarea.insertAfterSelection(response.responseText);}
    });
  }
</script>


        </div>

        <div id="local">
          <div class="local_top"></div>
          <div id="blog_owner">
            <div id="blog_owner_logo"><a href="http://zhupan.javaeye.com/"><img alt="温柔一刀的博客" class="logo" src="26427_files/e648acc6-56b7-482d-b86a-bf55968edd7a.jpg" title="温柔一刀的博客: 温柔一刀"></a></div>
            <div id="blog_owner_name">温柔一刀</div>
          </div>
          
          <div id="blog_actions">
            <ul>
              <li>浏览: 439779 次</li>
              <li>性别: <img alt="Icon_minigender_1" src="26427_files/icon_minigender_1.gif" title="男"></li>
              <li>来自: 上海</li>
              <li><img src="26427_files/offline.gif"></li>
              <li>
                <a href="http://zhupan.javaeye.com/blog/profile" class="profile">详细资料</a>
                <a href="http://zhupan.javaeye.com/blog/guest_book" class="guest_book">留言簿</a>
              </li>
              
            </ul>
            <h5>搜索本博客</h5>
            <form action="/blog/search" method="get">              <input class="text" id="query" name="query" style="margin-left: 10px; width: 110px;" type="text">
              <input class="submit" value="搜索" type="submit">
            </form>          </div>
          
            <div id="user_visits" class="clearfix">
              <h5>最近访客 <span style="font-weight: normal; font-size: 12px; padding-left: 30px;"><a href="http://zhupan.javaeye.com/blog/user_visits">&gt;&gt;更多访客</a></span></h5>
              
                <div class="user_visit">
                  <div class="logo"><a href="http://fcllingzi.javaeye.com/" target="_blank"><img alt="fcllingzi的博客" class="logo" src="26427_files/user-logo-thumb.gif" title="fcllingzi的博客: fcllingzi"></a></div>
                  <div class="left"><a href="http://fcllingzi.javaeye.com/" target="_blank">fcllingzi</a></div>
                </div>
              
                <div class="user_visit">
                  <div class="logo"><a href="http://dingzhaoxu.javaeye.com/" target="_blank"><img alt="dingzhaoxu的博客" class="logo" src="26427_files/user-logo-thumb.gif" title="dingzhaoxu的博客: "></a></div>
                  <div class="left"><a href="http://dingzhaoxu.javaeye.com/" target="_blank">dingzhaoxu</a></div>
                </div>
              
                <div class="user_visit">
                  <div class="logo"><a href="http://fqtrnt.javaeye.com/" target="_blank"><img alt="fqtrnt的博客" class="logo" src="26427_files/a9fb5f80-631a-4ccf-b392-01259f8a244b-thumb.jpg" title="fqtrnt的博客: fqtrnt"></a></div>
                  <div class="left"><a href="http://fqtrnt.javaeye.com/" target="_blank">fqtrnt</a></div>
                </div>
              
                <div class="user_visit">
                  <div class="logo"><a href="http://djjchobits.javaeye.com/" target="_blank"><img alt="djjchobits的博客" class="logo" src="26427_files/user-logo-thumb.gif" title="djjchobits的博客: "></a></div>
                  <div class="left"><a href="http://djjchobits.javaeye.com/" target="_blank">djjchobits</a></div>
                </div>
              
            </div>
          
                      <div id="blog_menu">
              <h5>博客分类</h5>
              <ul>
                <li><a href="http://zhupan.javaeye.com/">全部博客 (123)</a></li>
                
                  <li><a href="http://zhupan.javaeye.com/category/31388">action english  (2)</a></li>
                
                  <li><a href="http://zhupan.javaeye.com/category/31396">english (4)</a></li>
                
                  <li><a href="http://zhupan.javaeye.com/category/17339">ruby on rails (13)</a></li>
                
                  <li><a href="http://zhupan.javaeye.com/category/6740">Agile (6)</a></li>
                
                  <li><a href="http://zhupan.javaeye.com/category/5359">java (35)</a></li>
                
                  <li><a href="http://zhupan.javaeye.com/category/5360">open source   (17)</a></li>
                
                  <li><a href="http://zhupan.javaeye.com/category/5713">database (8)</a></li>
                
                  <li><a href="http://zhupan.javaeye.com/category/21794">Algorithms (1)</a></li>
                
                  <li><a href="http://zhupan.javaeye.com/category/7951">Linux (4)</a></li>
                
                  <li><a href="http://zhupan.javaeye.com/category/5492">other (17)</a></li>
                
                  <li><a href="http://zhupan.javaeye.com/category/29246">financing (4)</a></li>
                
                  <li><a href="http://zhupan.javaeye.com/category/13912">Business Intelligence(BI) (1)</a></li>
                
              </ul>
            </div>
            
              <div id="blog_album">
                <h5>我的相册</h5>
                <div class="picture_thumb" style="float: none;">
                  <div><span class="thumb_box"><a href="http://zhupan.javaeye.com/album"><img src="26427_files/d4817313-7183-341d-b1d6-f9d5b775ab46-thumb.jpg"></a></span></div>
                  <span>很黄，很暴力<br> <a href="http://zhupan.javaeye.com/album">共 14 张</a></span>
                </div>
              </div>
            
            
              <div id="guest_books">
                <h5>我的留言簿 <span style="font-weight: normal; font-size: 12px; padding-left: 20px;"><a href="http://zhupan.javaeye.com/blog/guest_book">&gt;&gt;更多留言</a></span></h5>
                <ul>
                  
                    <li>
                      在吗？导入excel文章是错误的。。。。<br>
                      -- by <a href="http://zhupan.javaeye.com/blog/guest_book#10949">eric_zhang_0712</a>
                    </li>
                  
                    <li>
                      public interface Plant {
	public void doS ...<br>
                      -- by <a href="http://zhupan.javaeye.com/blog/guest_book#7937">lspgdut</a>
                    </li>
                  
                    <li>
                      请教一下，如果域名+工程名FCKeditor作为访问url上传图片没问题，但是如果 ...<br>
                      -- by <a href="http://zhupan.javaeye.com/blog/guest_book#7573">sololodestar</a>
                    </li>
                  
                </ul>
              </div>
            
            <div id="blog_others">
              <h5>其他分类</h5>
              <ul>
                <li><a href="http://zhupan.javaeye.com/blog/favorite">我的收藏</a> (46)</li>
                                                  <li><a href="http://zhupan.javaeye.com/blog/news">我的新闻</a> (5) </li>
                                <li><a href="http://zhupan.javaeye.com/blog/forum">我的论坛帖子</a> (779)</li>
                <li><a href="http://zhupan.javaeye.com/blog/article">我的精华良好贴</a> (3)</li>
                
                  <li><a href="http://zhupan.javaeye.com/blog/solution">我解决的问题</a> (23) </li>
                              </ul>
            </div>
            
            <div id="recent_groups">
              <h5>最近加入圈子</h5>
              <ul>
                
                  <li><a href="http://mars.group.javaeye.com/">火星常驻JE办事处</a></li>
                
                  <li><a href="http://turingbook.group.javaeye.com/">图灵俱乐部</a></li>
                
                  <li><a href="http://ext.group.javaeye.com/">EXT</a></li>
                
                  <li><a href="http://hzjavaeyer.group.javaeye.com/">javascript研究小组</a></li>
                
                  <li><a href="http://wfp.group.javaeye.com/">函数式编程の道</a></li>
                
              </ul>
            </div>
            
              <div id="user_links">
                <h5>链接</h5>
                <ul>
                  
                    <li><a href="http://fengou.spaces.live.com/default.aspx" target="_blank">Feng Ou</a></li>
                  
                    <li><a href="http://www.designui.net/" target="_blank">genmous‘ blog</a></li>
                  
                    <li><a href="http://ice-cream.javaeye.com/" target="_blank">ice-cream</a></li>
                  
                    <li><a href="http://www.mlbsm.com/" target="_blank">美丽不是梦</a></li>
                  
                </ul>
              </div>
            
            
                          <div id="month_blogs">
                <h5>存档</h5>
                <ul>
                  
                    <li><a href="http://zhupan.javaeye.com/blog/monthblog/2009-07">2009-07</a> (1)</li>
                  
                    <li><a href="http://zhupan.javaeye.com/blog/monthblog/2008-11">2008-11</a> (2)</li>
                  
                    <li><a href="http://zhupan.javaeye.com/blog/monthblog/2008-10">2008-10</a> (2)</li>
                  
                  <li><a href="http://zhupan.javaeye.com/blog/monthblog_more">更多存档...</a></li>
                </ul>
              </div>
                        <div id="recent_comments">
              <h5>最新评论</h5>
              <ul>
                
                  <li>
                    <a href="http://zhupan.javaeye.com/blog/434444#comments" title="SNS，在中国，腾讯将是最后的赢家">SNS，在中国，腾讯将是最 ...</a><br>
                    QQ是最不愁盈利模式的。<br>
                    -- by <a href="http://rox.javaeye.com/" target="_blank">rox</a>
                  </li>
                
                  <li>
                    <a href="http://zhupan.javaeye.com/blog/434444#comments" title="SNS，在中国，腾讯将是最后的赢家">SNS，在中国，腾讯将是最 ...</a><br>
                    sslaowan 写道先有的QQ，后有的MSN你是在搞笑？<br>
                    -- by <a href="http://gmail.javaeye.com/" target="_blank">爱死不活</a>
                  </li>
                
                  <li>
                    <a href="http://zhupan.javaeye.com/blog/434444#comments" title="SNS，在中国，腾讯将是最后的赢家">SNS，在中国，腾讯将是最 ...</a><br>
                    sirxenofex 写道一直觉得QQ作为一个通信软件很山寨，但是MSN就是流行不 ...<br>
                    -- by <a href="http://sslaowan.javaeye.com/" target="_blank">sslaowan</a>
                  </li>
                
                  <li>
                    <a href="http://zhupan.javaeye.com/blog/434444#comments" title="SNS，在中国，腾讯将是最后的赢家">SNS，在中国，腾讯将是最 ...</a><br>
                    我用qq原因在于我希望一个帐号可以干N多事情，N多事情可以关联起来。
我用手机也是 ...<br>
                    -- by <a href="http://hgyxb.javaeye.com/" target="_blank">hgyxb</a>
                  </li>
                
                  <li>
                    <a href="http://zhupan.javaeye.com/blog/434444#comments" title="SNS，在中国，腾讯将是最后的赢家">SNS，在中国，腾讯将是最 ...</a><br>
                    fuzan 写道昨天还有同学在公司用MSN，呵呵，不会用，不好用，客户体验不好。虽 ...<br>
                    -- by <a href="http://iaimstar.javaeye.com/" target="_blank">iaimstar</a>
                  </li>
                
              </ul>
            </div>
            <div id="comments_top">
              <h5>评论排行榜</h5>
              <ul>
                
                  <li><a href="http://zhupan.javaeye.com/blog/434444" title="SNS，在中国，腾讯将是最后的赢家">SNS，在中国，腾讯将是最后的赢家</a></li>
                
                  <li><a href="http://zhupan.javaeye.com/blog/266438" title="寻一本书《探索需求——设计前的质量》">寻一本书《探索需求——设计前的质量》</a></li>
                
                  <li><a href="http://zhupan.javaeye.com/blog/259856" title="Office Matters ------ Part A">Office Matters ------ Part A</a></li>
                
                  <li><a href="http://zhupan.javaeye.com/blog/271662" title="Spring Integration 1.0 发布">Spring Integration 1.0 发布</a></li>
                
              </ul>
            </div>
            <div id="rss">
              <ul>
                <li><a href="http://zhupan.javaeye.com/rss" target="_blank"><img alt="Rss" src="26427_files/rss.png"></a></li>
                <li><a href="http://fusion.google.com/add?feedurl=http://zhupan.javaeye.com/rss" target="_blank"><img alt="Rss_google" src="26427_files/rss_google.gif"></a></li>
                <li><a href="http://www.zhuaxia.com/add_channel.php?url=http://zhupan.javaeye.com/rss" target="_blank"><img alt="Rss_zhuaxia" src="26427_files/rss_zhuaxia.gif"></a></li>
                <li><a href="http://www.xianguo.com/subscribe.php?url=http://zhupan.javaeye.com/rss" target="_blank"><img alt="Rss_xianguo" src="26427_files/rss_xianguo.jpg"></a></li>
                <li><a href="http://www.google.com/search?hl=zh-CN&amp;q=RSS">[什么是RSS?]</a></li>
              </ul>
            </div>
            <div class="local_bottom"></div>
          
        </div>
      </div>

      <div id="footer" class="clearfix">
        <div id="copyright">
          <hr>
          声明：JavaEye文章版权属于作者，受法律保护。没有作者书面许可不得转载。若作者同意转载，必须以超链接形式标明文章原始出处和作者。<br>
          © 2003-2009 JavaEye.com.   All rights reserved. 上海炯耐计算机软件有限公司 [ 沪ICP备05023328号 ]
        </div>
      </div>
    </div>
    
<script type="text/javascript">
  document.write("<img src='http://stat.javaeye.com/?url="+ encodeURIComponent(document.location.href) + "&referrer=" + encodeURIComponent(document.referrer) + "&logged_in=no" + "' width='0' height='0' />");
</script><img src="26427_files/a.gif" width="0" height="0">
    
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script><script src="26427_files/ga.js" type="text/javascript"></script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-7060780-1");
pageTracker._trackPageview();
</script>
      
  </body></html>